package indi.luckly.service.impl;

import cn.hutool.core.date.DateUtil;
import indi.luckly.common.request.SignInRequestParams;
import indi.luckly.common.response.SignInResponseData;
import indi.luckly.common.response.ResponseResult;;
import indi.luckly.entity.Sign;
import indi.luckly.common.key.ErrorKey;
import indi.luckly.mapper.SignMapper;
import indi.luckly.service.ISignService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wangchengyuan
 * @since 2021-12-26
 */
@Service
public class SignServiceImpl extends ServiceImpl<SignMapper, Sign> implements ISignService {

    @Autowired
    @Qualifier("signMapper")
    private SignMapper signMapper;

    @Override
    public ResponseResult<SignInResponseData> signIn(SignInRequestParams params) {
        Sign sign = new Sign();
        sign.setUserNumber(sign.getUserNumber());
        Date date = DateUtil.endOfDay(new Date());
        sign.setCreateDate(DateUtil.endOfDay(date));

        Map<String, Object> map = new HashMap<>();
        map.put("user_name", params.getUserNumber());
        map.put("date", date);
        List<Sign> signs = signMapper.selectByMap(map);
        if (signs.size() > 0) {
            SignInResponseData signInResponseData = new SignInResponseData();
            return ResponseResult.fail(ErrorKey.DIFFERENT_PASSWORDS,signInResponseData);
        } else {
            SignInResponseData signInResponseData = new SignInResponseData();
            signInResponseData.setDate(date.toString());
            return ResponseResult.ok(signInResponseData);
        }

    }
}
